package com.alm.handle.util;

import org.springframework.util.StringUtils;

import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;

public class ByteUtils {

    /**
     * 十六进制字符串转换为byte[]
     *
     * @param str
     * @return
     */
    public static byte[] hexStrToBytes(String str) {
        if (str == null || str.trim().equals("")) {
            return new byte[0];
        }
        str = StringUtils.replace(str, ",", "");
        final byte[] bytes = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2; i++) {
            final String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }
        return bytes;
    }

    /**
     * 取中间值
     *
     * @param start
     * @param end
     * @param data
     * @return
     */
    public static byte[] getByteByByte(int start, int end, byte[] data) {
        byte[] bytes = new byte[end - start];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = data[Math.min(start + i - 1,data.length-1)];
        }
        return bytes;
    }
    public   static String  getVersionString(int index, List<Integer> datas, int len)  {
        byte [] dataByte=new byte[len];
        for (int i = 0; i < len; i++) {
            dataByte[i]=datas.get(index).byteValue();
            index+=1;
        }
        try {
            return  new String(dataByte,"UTF8");
        }catch (Exception e){
            return "";
        }
    }
    public static byte[] segmentationData(int lth, int start, byte[] data) {
        byte[] bytes = new byte[lth];
        System.arraycopy(data, start, bytes, 0, bytes.length);
        return bytes;
    }

    /**
     * 获取集合
     *
     * @param start
     * @param end
     * @param data
     * @return
     */
    public static List<Integer> getListByteByByte(int start, int end, byte[] data) {
        List<Integer> bytes = new ArrayList<>();
        int maxIndex = data.length - 1;
        for (int i = 0; i < end - start; i++) {
            bytes.add(data[Math.min(start + i - 1,maxIndex)] & 0xff);
        }
        return bytes;
    }

    /**
     * byte转字符
     *
     * @param data
     * @param radix
     * @return
     */
    public static String getStringByByte(byte[] data, int radix) {
        return new BigInteger(1, data).toString(radix);
    }

    /**
     * byte转 int
     *
     * @return
     */
    public static int bytes2IntBig(byte[] bytes) {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            str.append(String.format("%02x", bytes[i]));
        }
        return Integer.parseInt(str.toString(), 16);
    }

    public static Long bytes2LongBig(byte[] bytes) {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            str.append(String.format("%02x", bytes[i]));
        }
        return Long.parseLong(str.toString(), 16);
    }

    /**
     * byte数组转short
     *
     * @param bytes
     * @return
     */
    public static short bytes2ShortBig(byte[] bytes) {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            str.append(String.format("%02x", bytes[i]));
        }
        return (short) Integer.parseInt(str.toString(), 16);
    }

    public static String arrayToString(List<Integer> bytes, int start, int end) {
        StringBuffer str = new StringBuffer();
        for (int i = start; i < end; i++) {
            str.append(String.format("%02x", bytes.get(i).byteValue()));
        }
        return str.toString();
    }

    /**
     * 取第二位 第三位 右移五位 获取长度
     *
     * @param date
     * @return
     */
    public static byte getLenth(Byte date) {
        byte lth = (byte) ((date & 0x60) >> 5);
        return lth;
    }

    /**
     * 右移五位 获取长度
     *
     * @param date
     * @return
     */
    public static byte getstatus(byte date) {
        byte lth = (byte) ((date & 0x80) >> 7);
        return lth;
    }

    /**
     * 指令类型
     *
     * @param date
     * @return
     */
    public static byte getinType(Byte date) {
        byte lth = (byte) (date & 0x1f);
        return lth;
    }

    /**
     * 转数组 高位在前 低位在后
     *
     * @param data
     * @param start
     * @return
     */
    public static int conversionInt(List<Integer> data, int start) {
        int value;
        value = (int) ((data.get(start).byteValue() & 0xFF) |
                ((data.get(start + 1).byteValue() & 0xFF) << 8) |
                ((0 & 0xFF) << 16) |
                ((0 & 0xFF) << 24));
        return value;
    }

    public static byte[] getByteArrByString(String data) {
        byte[] byteData = Base64.getDecoder().decode(data);
        return byteData;
    }

    public static String getStringByByteArr(byte[] data) {
        String strdata = Base64.getEncoder().encodeToString(data);
        return strdata;
    }

    /**
     * byte数组转换成十六进制字符串
     *
     * @param bytes 待转换byte
     * @return java.lang.String 转换后十六进制字符串
     * @author: 詹泽峰
     * @date: 2024/7/18 16:58
     */
    public static String bytes2String(byte[] bytes) {
        StringBuilder out = new StringBuilder();

        for (byte aByte : bytes) {
            out.append(String.format("%02x", aByte));
        }

        return out.toString();
    }

    /**
     * byte数组转换成Short类型
     *
     * @param bytes 待转换byte[]
     * @param isBig true：大端模式，false：小端模式
     * @return short 转换后short
     * @author: 詹泽峰
     * @date: 2024/7/17 14:30
     */
    public static short bytes2Short(byte[] bytes, boolean isBig) {
        if (isBig) {
            return (short) (((bytes[0] << 8) | bytes[1] & 0xff));
        } else {
            return (short) (((bytes[1] << 8) | bytes[0] & 0xff));
        }
    }

    public static String bytesToString(byte[] bytes) {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            str.append(String.format("%02x", bytes[i]));
        }
        return str.toString();
    }

    public static String bytesToString(List bytes) {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < bytes.size(); i++) {
            str.append(String.format("%02x", bytes.get(i)));
        }
        return str.toString();
    }

    public static String compensationZero(String old, int lenth) {
        int bc = lenth - old.length();
        if (bc <= 0) {
            return old;
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < bc; i++) {
            buffer.append("0");
        }
        buffer.append(old);
        return buffer.toString();
    }
}
